#!/usr/bin/env bash

DEFAULT_HOST="https://gitee.com/tj123/common/raw/master"
HOST="http://<%= $host %>"
if [[ "$HOST" =~ ^http:\/\/\<.*\>$ ]];then
  HOST=$DEFAULT_HOST
fi

releasever="$(cat /etc/redhat-release|grep -o '[0-9\.]\+'|grep -o '^[0-9]')"
USER="oracle"
PASSWORD="123456"
IS_ORACLE="false"
AUTO_START="false"
INSTALL_FONT="false"
FONT_URL="https://github.com/tj123/mirrors/raw/master"

for pm in "$@"; do
  case "$pm" in
    --password=*)
      PASSWORD="${pm#*=}";;
    --user=*)
      USER="${pm#*=}";;
    --font-url=*)
      FONT_URL="${pm#*=}";;
    --install-font)
      INSTALL_FONT="true";;
    --oracle)
      IS_ORACLE="true";;
    --auto-start)
      AUTO_START="true";;
    *)
    ;;
  esac
done

# 安装 xfce 和 vnc
install_xfcevnc(){
  yum install -y epel-release
  yum groupinstall -y "X Window system"
  yum groupinstall -y xfce
  yum install -y tigervnc-server
  if [ "$releasever" == "7" ]; then
    sed -e "s/<USER>/$USER/" \
      -e "s+^\s*ExecStartPre.*+ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>\&1 || :;rm -rf /tmp/.X*-lock /tmp/.ICE-unix/ /tmp/.X11-unix/ /tmp/.XIM-unix/'+" \
      /usr/lib/systemd/system/vncserver\@.service > /usr/lib/systemd/system/vncserver\@:1.service
  else
    sed -e "s/\s*#\?\s*VNCSERVERS=.*/VNCSERVERS=\"1:$USER\"/g" \
      -e "s/\s*#\?\s*VNCSERVERARGS\[2\]=.*/VNCSERVERARGS[2]=\"-geometry 800x600 -nolisten tcp -localhost\"/g" \
      -i /etc/sysconfig/vncservers
  fi
  ps -ef|grep /usr/bin/Xvnc|grep -v grep|awk '{print $2}'|xargs kill -9 >/dev/null 2>&1
  rm -rf $HOME/.vnc
  rm -rf /tmp/.X*-lock
  # 用户不存在
  if ! id "$USER" 1>/dev/null 2>&1; then
    useradd "$USER"
    echo -e "$PASSWORD\n$PASSWORD\n"|passwd $USER >/dev/null
    chmod +w /etc/sudoers
    sed "/^\s*root\s*ALL=.*/a$USER  ALL=(ALL)       ALL" -i /etc/sudoers
    chmod -w /etc/sudoers
  fi
  su "$USER" -c "curl -s $HOST/vnc/install-user.sh|bash -s -- --password=$PASSWORD"
  systemctl daemon-reload
  if [ "$releasever" == "7" ]; then
    service vncserver@:1 restart
    [ "$AUTO_START" == "true" ] && chkconfig vncserver@:1 on
  else
    service vncserver restart
    [ "$AUTO_START" == "true" ] && chkconfig vncserver on
  fi
}

# 安装雅黑字体
install_yahei(){
  [ -d /usr/share/fonts/ ] || mkdir -p /usr/share/fonts/
  curl -SL# $FONT_URL/msyhl.ttc -o /usr/share/fonts/msyhl.ttc
}

# 安装 ubunto mono
install_mono(){
  [ -d /usr/share/fonts/ ] || mkdir -p /usr/share/fonts/
  curl -SL# $FONT_URL/UbuntuMono-R.ttf -o /usr/share/fonts/UbuntuMono-R.ttf
}

# 安装 Oracle 使用的宋体
install_zysong(){
  [ -d /usr/share/fonts/zh_CN/TrueType ] || mkdir -p /usr/share/fonts/zh_CN/TrueType
  curl -SL# $FONT_URL/zysong.ttf -o /usr/share/fonts/zh_CN/TrueType/zysong.ttf
}

# 安装字体文件
install_font(){
  if [ "$IS_ORACLE" == "true" ]; then
    install_zysong
  else
    install_yahei
    install_mono
  fi
}

# 配置自启动
auto_start(){
  [ "$releasever" == "7" ] && chkconfig vncserver@:1 on || chkconfig vncserver on
}

for pm in "$@"; do
  case "$pm" in
    install)
      install_xfcevnc; shift;;
    install-font)
      install_font; shift;;
    auto-start)
      auto_start; shift;;
    install-yahei)
      install_yahei; shift;;
    install-mono)
      install_mono; shift;;
    install-zysong)
      install_zysong; shift;;
    *)
      if [[ "$pm" =~ ^-- ]]; then
        shift
      else
        echo "未知命令 $pm"
        echo "查看更多命令请运行: $0 --help"
      fi
      ;;
  esac
done
